#ifndef SERIALCHARX_H
#define SERIALCHARX_H

#include <QtWidgets/QMainWindow>
#include <QCloseEvent>
#include <QTimer>
#include <QTreeWidgetItem>
#include <QModelIndex>
#include <QMenu>
#include <QAction>
#include <QToolBar>
#include <QPushButton>
#include "ui_serialcharx.h"
#include "curveitem.h"
#include "curvedockwin.h"
#include "serialif.h"
#include "comportqcombobox.h"
#include "pointdata.h"
#include "csvdecoder.h"
#include "qwt_plot.h"
#include "plot.h"
#include "consoledockwin.h"
#include <QTime>

#define X_Y_LOCK_BT_SIZE		(32)
#define APP_NAME				("SerialCharX")

class SerialCharX : public QMainWindow
{
	Q_OBJECT

public:
	SerialCharX(QWidget *parent = 0);
	~SerialCharX();

private:
	void createActions(void);
	void createMenus(void);
	void createToolBars(void);
	void createDockWin(void);
	void createMainPlotWin(void);

	void closeEvent(QCloseEvent *event);
	void writeSettings(void);
	void readSettings(void);
	Ui::SerialCharXClass ui;
	void mainLoop(void);

	// File menu actions
	void newFile(void);
	void openFile(void);
	void save(void);
	void saveAs(void);
	void exportTo(void);
	void printTo(void);

	// Plot actions
	void startPlot(void);
	void stopPlot(void);
	void plotSetup(void);
	void plotCanvasColorSet(void);
	void plotAutoScale(void);
	void plotDel(void);
	void plotMax(void);
	void plotMin(void);

	// View actions
	void curveDockWinViewOnOff(void);
	void consoleDockWinViewOnOff(void);

	// File save
	void setCurrentFile(const QString &fileName);
	bool loadFile(const QString &fileName);
	bool saveFile(const QString &fileName);

private slots:
	void plotOnRecvPoint(PointData point);
	void on_x_axis_lock(void);
	void on_y_axis_lock(void);
	void on_plot_grid_sw(void);
	void on_recv_io_data(QByteArray);

private:
	// Menu
	QMenu *m_fileMenu;
	QMenu *m_editMenu;
	QMenu *m_viewMenu;
	QMenu *m_optionMenu;
	QMenu *m_helpMenu;

	// Actions
	QAction *m_newAction;
	QAction *m_openAction;
	QAction *m_saveAction;
	QAction *m_saveAsAction;
	QAction *m_exportAction;
	QAction *m_printAction;
	QAction *m_exitAction;
	
	QAction *m_startAction;
	QAction *m_stopAction;
	QAction *m_plotSetupAction;
	QAction *m_cutAction;
	QAction *m_copyAction;
	QAction *m_pasteAction;
	QAction *m_deleteAction;
	QAction *m_showGridAction;
	QAction *m_autoRecalcAction;
	QAction *m_plotMaxAction;
	QAction *m_plotMinAction;
	
	// View actions
	QAction *m_curveDockWinViewAction;
	QAction *m_consoleDockWinViewAction;

	QAction *m_aboutAction;
	QAction *m_aboutQtAction;

	QAction *m_plotAutoScaleAction;
	QAction *m_plotDelAction;
	QAction *m_plotCanvas;
	QPushButton *m_plotLockXButton;
	QPushButton *m_plotLockYButton;
	QPushButton *m_plotGridSw;

	// ToolBar
	QToolBar *m_fileToolBar;
	QToolBar *m_editToolBar;
	QToolBar *m_serPortToolBar;
	QToolBar *m_PlotToolBar;
	comPortQComboBox *m_ser_port_list;
	QComboBox *m_ser_bdrate_list;

	QString m_curFile;
	QString m_curDir;

	// Dock windows
	CurveitemDockWin *m_curveDockWin;
	ConsoleDockWin *m_consoleDockWin;

	// Seirial port interface
	SerialIo *m_ser;
	CsvDecoder *m_decoder;

	// Main plot
	Plot *m_main_plot;

	// Main loop timer
	QTimer m_main_loop_timer;

	QTime m_calc_time;

};

#endif // SERIALCHARX_H
